package com.concurrent.threadpool;

import java.util.concurrent.*;

/**
 * @Date 2020/7/13 15:38
 * @Author admin
 * @Description
 */
public class FutureTaskDemo {
    //T2任务的FutureTask
     FutureTask<String> ft2 = new FutureTask<>(new T2Task());
    //T1任务的FutureTask
    FutureTask<String> ft1 = new FutureTask<>(new T1Task(ft2));

    FutureTask<Integer> f1 = new FutureTask<>(new Thread1());
    FutureTask<Integer> f2 = new FutureTask<>(new Thread2());
    FutureTask<Integer> f3 = new FutureTask<>(new Thread3());

    ExecutorService executorService = Executors.newFixedThreadPool(3);
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTaskDemo demo = new FutureTaskDemo();
//        ExecutorService ss = Executors.newFixedThreadPool(3);
//        //线程T1执行任务ft1
//        Thread t1 = new Thread(demo.ft1);
//        t1.start();
//        TimeUnit.SECONDS.sleep(1);
//        //线程T2执行任务ft2
//        Thread t2 = new Thread(demo.ft2);
//        t2.start();
//        //等待线程T1执行结果
//        System.out.println(demo.ft1.get());
//        System.out.println(demo.ft2.get());

//        new Thread(demo.f1).start();
        System.out.println("f1====="+demo.f1.get());
//        ss.submit(new Thread(demo.f2));
        new Thread(demo.f2).start();
        new Thread(demo.f3).start();
        demo.test();
    }
    public void test(){
        Thread1 thread1 = new Thread1();
        Thread2 thread2 = new Thread2();
        Thread3 thread3 = new Thread3();
//        executorService.submit(thread1);
        executorService.execute(new FutureTask<Integer>(thread1));
        executorService.submit(thread2);
        executorService.execute(new FutureTask<Integer>(thread2));
//        executorService.submit(thread3);
        executorService.execute(new FutureTask<Integer>(thread3));
        executorService.shutdown();
    }

    class T1Task implements Callable<String> {
        FutureTask<String> ft2;

        //T1任务需要T2任务的FutureTask
        T1Task(FutureTask<String> ft2) {
            this.ft2 = ft2;
        }
        @Override
        public String call() throws Exception {
            System.out.println("洗水壶");
            TimeUnit.SECONDS.sleep(1);
            System.out.println("烧开水");
            TimeUnit.SECONDS.sleep(15);
            //获取T2线程的茶叶
            String tf = ft2.get();
            System.out.println("T1:拿到茶叶:" + tf);
            System.out.println("泡茶....");
            return "上茶：" + tf;
        }
    }

    class T2Task implements Callable<String> {
        //T2Task需要执行的任务
        //洗茶壶，洗茶杯，拿茶叶
        @Override
        public String call() throws Exception {
            System.out.println("T2:洗茶壶");
            TimeUnit.SECONDS.sleep(1);
            System.out.println("T2:洗茶杯");
            TimeUnit.SECONDS.sleep(2);
            System.out.println("T2:拿茶叶");
            TimeUnit.SECONDS.sleep(1);
            return "龙井";
        }
    }



    class Thread1 implements Callable<Integer>{
        //查询s1并保存
        @Override
        public Integer call() throws Exception {
            int r1 = getPriceByS1();
            return save(r1);
        }

        private int save(int r1) {
            System.out.println("save....s1");
            return 1;
        }

        private int getPriceByS1() {
            System.out.println("s1价格为1");
            return 1;
        }
    }
    class Thread2 implements Callable<Integer>{

        @Override
        public Integer call() throws Exception {
            int r2 = getPriceByS2();
            return save(r2);
        }

        private int save(int r2) {
            System.out.println("save....s2");
            return 1;
        }

        private int getPriceByS2() {
            System.out.println("s2价格为2");
            return 2;
        }
    }

    class Thread3 implements Callable<Integer>{
        @Override
        public Integer call() throws Exception {
            int r3 = getPriceByS3();
            return save(r3);
        }

        private int save(int r3) {
            System.out.println("save....s3");
            return 3;
        }

        private int getPriceByS3() {
            System.out.println("s3价格为3");
            return 3;
        }
    }

}
